package gui;
//penis
//package graphics;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import meal.*;
import util.*;

public class IngredientGUI extends JFrame implements ActionListener
{


	int recipeCounter = 1;
	boolean isEdit = false;
	boolean isInstructionEdit = false;
	private DefaultListModel recipeModel = new DefaultListModel();
	private ArrayList<Ingredient> tempIngredientList = new ArrayList<Ingredient>();
	private ArrayList<Direction> tempInstructionList = new ArrayList<Direction>();
	private DefaultListModel ingredientModel = new DefaultListModel();
	private DefaultListModel instructionModel = new DefaultListModel();
	JFrame mainframe = new JFrame();
	JPanel ingredientAdder, recipeAdder, login, userPref;


	//Buttons for lineMode
	JButton lineModeLogin, lineModeSkip;
	//Labels of LineMode
	JLabel loginInstructions, usernameLabel, passwordLabel;
	//Textfields of LineMode
	JTextField userName;
	JPasswordField password;
	//Panels of LineMode
	JPanel labelPanel, textFieldPanel, buttonPanel;

	//Panels for UserPref
	JPanel eatDays = new JPanel();
	JPanel grocDays = new JPanel();
	JPanel eatDaysInstructions = new JPanel();
	JPanel grocDaysInstructions = new JPanel();
	JPanel grocFreqInstructions = new JPanel();
	//Buttons for UserPref
	JButton userPrefNext = new JButton("Done");
	//Labels for UserPref
	JLabel prefEatDaysLabel = new JLabel("Please indicate which days you wish to have meals scheduled: ");
	JLabel prefGrocDaysLabel = new JLabel("Please indicate which days you wish to buy groceries: ");
	JLabel prefGrocWeeksLabel = new JLabel("Please indicate how often you wish to go shopping: ");
	//Checkboxes for Eating days UserPref
	JCheckBox prefEatMondayButton = new JCheckBox("Monday");
	JCheckBox prefEatTuesdayButton = new JCheckBox("Tuesday");
	JCheckBox prefEatWednesdayButton = new JCheckBox("Wednesday");
	JCheckBox prefEatThursdayButton = new JCheckBox("Thursday");
	JCheckBox prefEatFridayButton = new JCheckBox("Friday");
	JCheckBox prefEatSaturdayButton = new JCheckBox("Saturday");
	JCheckBox prefEatSundayButton = new JCheckBox("Sunday");
	//Checkboxes for Grocery days UserPref
	JCheckBox prefBuyMondayButton = new JCheckBox("Monday");
	JCheckBox prefBuyTuesdayButton = new JCheckBox("Tuesday");
	JCheckBox prefBuyWednesdayButton = new JCheckBox("Wednesday");
	JCheckBox prefBuyThursdayButton = new JCheckBox("Thursday");
	JCheckBox prefBuyFridayButton = new JCheckBox("Friday");
	JCheckBox prefBuySaturdayButton = new JCheckBox("Saturday");
	JCheckBox prefBuySundayButton = new JCheckBox("Sunday");
	//Comboboxes for Grocery oftenness UserPref
	String[] groceryFrequency = {"Once a Week", "Once Every Two Weeks", "Once a Month"};
	JComboBox prefGrocFrequency = new JComboBox(groceryFrequency);


	//Labels for RecipeAdder
	JLabel recipeAdderInstructions = new JLabel("Recipe List");
	//Buttons for RecipeAdder
	JButton recipeAdderButtonNew ;
	JButton recipeAdderButtonEdit;
	JButton recipeAdderButtonDelete;
	JButton recipeAdderButtonInputFile;
	JButton recipeAdderButtonSave;
	JButton recipeAdderButtonClose;
	//JList for RecipeAdder
	JList recipeAdderList = new JList(recipeModel);
	//JScrollPane for RecipeAdder
	JScrollPane recipeAdderScrollPane = new JScrollPane(recipeAdderList);

	//Buttons for IngredientAdder

	JButton ingredientAdd, ingredientEdit, ingredientDelete, instructionAdd;
	JButton instructionEdit, instructionDelete, ingredientAdderSave, ingredientAdderClose;
	JButton up, down;

	//TextFields for IngredientAdder
	JTextField recipeName, ingredientQuantity, ingredientName, instructionStep;
	//Labels for IngredientAdder
	JLabel recipeNameLabel, ratingLabel, quantityLabel, unitLabel, ingredientLabel;
	JLabel ingredientListLabel, instructionStepLabel, instructionLabel;
	//ComboBox for IngredientAdder
	String [] scale = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
	String[] unitMeasure = {null,"Tsp","Tbsp", "Cup","Ounce", "Pound", "Quart", "Pint", "Gallon", "Add Your Own"};
	JComboBox rating = new JComboBox(scale);
	JComboBox units = new JComboBox(unitMeasure);


	//Lists for IngredientAdder
	JList ingredientList = new JList(ingredientModel);
	JList instructionList = new JList(instructionModel);
	JScrollPane ingredientListScroll = new JScrollPane(ingredientList);
	JScrollPane instructionListScroll = new JScrollPane(instructionList);

	public void createIngredient()
	{
		units.setPreferredSize(new Dimension(110, 20));
		units.setEditable(true);
		rating.setPreferredSize(new Dimension(40, 20));
		ingredientList.setBackground(new Color(255,255,255));
		ingredientListScroll.setPreferredSize(new Dimension(360,135));
		instructionListScroll.setPreferredSize(new Dimension(360,135));
		instructionStep.setPreferredSize(new Dimension(280, 20));
		instructionAdd.setPreferredSize(new Dimension(60, 20));

		ingredientAdder = new JPanel();
		SpringLayout ingredientAdderLayout = new SpringLayout();
		ingredientAdder.setLayout(ingredientAdderLayout);

		ingredientAdder.add(recipeNameLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, recipeNameLabel, 10, SpringLayout.WEST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, recipeNameLabel, 10, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(recipeName);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, recipeName, 6, SpringLayout.EAST, recipeNameLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, recipeName, 10, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ratingLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ratingLabel, 25, SpringLayout.EAST, recipeName);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ratingLabel, 10, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(rating);
		ingredientAdderLayout.putConstraint(SpringLayout.EAST, rating, 50, SpringLayout.EAST, ratingLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, rating, 10, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(quantityLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, quantityLabel, 10, SpringLayout.WEST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, quantityLabel, 55, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientQuantity);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientQuantity, 4, SpringLayout.EAST, quantityLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientQuantity, 55, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(unitLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, unitLabel, 10, SpringLayout.EAST, ingredientQuantity);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, unitLabel, 55, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(units);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, units, 6, SpringLayout.EAST, unitLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, units, 55, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientLabel, 10, SpringLayout.EAST, units);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientLabel, 55, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientName);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientName, 4, SpringLayout.EAST, ingredientLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientName, 55, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientAdd);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientAdd, 10, SpringLayout.EAST, ingredientName);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientAdd, 55, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientListLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientListLabel, 10, SpringLayout.WEST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientListLabel, 84, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientListScroll);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientListScroll, 10, SpringLayout.WEST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientListScroll, 108, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientEdit);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientEdit, 12 , SpringLayout.EAST, ingredientListScroll);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientEdit, 130, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(ingredientDelete);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientDelete, 12 , SpringLayout.EAST, ingredientListScroll);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, ingredientDelete, 50, SpringLayout.NORTH, ingredientEdit);

		ingredientAdder.add(instructionStepLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, instructionStepLabel, 10, SpringLayout.WEST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, instructionStepLabel, 255, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(instructionStep);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, instructionStep, 105, SpringLayout.WEST, instructionStepLabel);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, instructionStep, 255, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(instructionAdd);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, instructionAdd, 10, SpringLayout.EAST, ingredientName);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, instructionAdd, 255, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(instructionListScroll);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, instructionListScroll, 10, SpringLayout.WEST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, instructionListScroll, 285, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(instructionEdit);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, instructionEdit, 12 , SpringLayout.EAST, instructionListScroll);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, instructionEdit, 315, SpringLayout.NORTH, ingredientAdder);

		ingredientAdder.add(instructionDelete);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, instructionDelete, 12 , SpringLayout.EAST, instructionListScroll);
		ingredientAdderLayout.putConstraint(SpringLayout.NORTH, instructionDelete, 50, SpringLayout.NORTH, instructionEdit);

		ingredientAdder.add(ingredientAdderClose);
		ingredientAdderLayout.putConstraint(SpringLayout.WEST, ingredientAdderClose, 10, SpringLayout.WEST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.SOUTH, ingredientAdderClose, -10, SpringLayout.SOUTH, ingredientAdder);

		ingredientAdder.add(ingredientAdderSave);
		ingredientAdderLayout.putConstraint(SpringLayout.EAST, ingredientAdderSave, -10, SpringLayout.EAST, ingredientAdder);
		ingredientAdderLayout.putConstraint(SpringLayout.SOUTH, ingredientAdderSave, -10, SpringLayout.SOUTH, ingredientAdder);

	}
	public void createIngredientTextField()
	{
		recipeName = new JTextField();
		recipeName.setPreferredSize(new Dimension(280, 23));

		ingredientQuantity = new JTextField();
		ingredientQuantity.setPreferredSize(new Dimension(35, 23));

		ingredientName = new JTextField();
		ingredientName.setPreferredSize(new Dimension(70, 23));
		instructionStep = new JTextField();
	}
	public void createIngredientButton()
	{
		ingredientAdd = new JButton("Add");
		ingredientAdd.setPreferredSize(new Dimension(60, 20));
		ingredientEdit = new JButton("Edit");
		ingredientEdit.setPreferredSize(new Dimension(80, 25));
		ingredientDelete = new JButton("Delete");
		ingredientDelete.setPreferredSize(new Dimension(80, 25));
		instructionAdd = new JButton("Add");
		instructionAdd.setPreferredSize(new Dimension(60, 20));
		instructionEdit = new JButton("Edit");
		instructionEdit.setPreferredSize(new Dimension(80, 25));
		instructionDelete = new JButton("Delete");
		instructionDelete.setPreferredSize(new Dimension(80, 25));
		ingredientAdderSave = new JButton("Save and Exit");
		ingredientAdderClose = new JButton("Cancel");
		up = new JButton("Up");
		down = new JButton("Down");

		ingredientAdd.addActionListener(this);
		ingredientEdit.addActionListener(this);
		ingredientDelete.addActionListener(this);
		instructionAdd.addActionListener(this);
		instructionStep.addActionListener(this);
		instructionEdit.addActionListener(this);
		instructionDelete.addActionListener(this);
		ingredientAdderSave.addActionListener(this);
		ingredientAdderClose.addActionListener(this);
		ingredientName.addActionListener(this);
		up.addActionListener(this);
		down.addActionListener(this);
	}
	public void createIngredientLabel()
	{
		recipeNameLabel = new JLabel("Name:");
		ratingLabel = new JLabel("Rating:");
		quantityLabel = new JLabel("Quantity:");
		unitLabel = new JLabel("Unit:");
		ingredientLabel = new JLabel("Ingredient:");
		ingredientListLabel = new JLabel("Ingredient List");
		instructionLabel = new JLabel("Instructions");
		instructionStepLabel = new JLabel("Instruction Step:");
	}
	public void createRecipeButton()
	{
		recipeAdderButtonNew = new JButton ("New");
		recipeAdderButtonEdit = new JButton ("Edit");
		recipeAdderButtonDelete = new JButton ("Delete");
		recipeAdderButtonInputFile = new JButton ("Input File");
		recipeAdderButtonSave = new JButton ("Save");
		recipeAdderButtonClose = new JButton ("Close");


		recipeAdderButtonNew.addActionListener(this);
		recipeAdderButtonEdit.addActionListener(this);
		recipeAdderButtonDelete.addActionListener(this);
		recipeAdderButtonInputFile.addActionListener(this);
		recipeAdderButtonSave.addActionListener(this);
		recipeAdderButtonClose.addActionListener(this);
	}
	public void createRecipeAdderPanel()
	{
		SpringLayout recipeAdderLayout = new SpringLayout();

		recipeAdderButtonNew.setPreferredSize(new Dimension(75,25));
		recipeAdderButtonEdit.setPreferredSize(new Dimension(75,25));
		recipeAdderButtonDelete.setPreferredSize(new Dimension(75,25));
		recipeAdderScrollPane.setPreferredSize(new Dimension(320,360));

		recipeAdderButtonInputFile.setPreferredSize(new Dimension(100,25));
		recipeAdderButtonSave.setPreferredSize(new Dimension(75,25));
		recipeAdderButtonClose.setPreferredSize(new Dimension(100,25));

		recipeAdder = new JPanel();
		recipeAdder.setLayout(recipeAdderLayout);

		recipeAdder.add(recipeAdderInstructions);
		recipeAdderLayout.putConstraint(SpringLayout.WEST, recipeAdderInstructions, 20, SpringLayout.WEST, recipeAdder);
		recipeAdderLayout.putConstraint(SpringLayout.NORTH, recipeAdderInstructions, 15, SpringLayout.NORTH, recipeAdder);


		recipeAdder.add(recipeAdderScrollPane);
		recipeAdderLayout.putConstraint(SpringLayout.WEST, recipeAdderScrollPane, 20, SpringLayout.WEST, recipeAdder);
		recipeAdderLayout.putConstraint(SpringLayout.NORTH, recipeAdderScrollPane, 35, SpringLayout.NORTH, recipeAdder);


		recipeAdder.add(recipeAdderButtonNew);
		recipeAdderLayout.putConstraint(SpringLayout.WEST, recipeAdderButtonNew, 30, SpringLayout.EAST, recipeAdderScrollPane);
		recipeAdderLayout.putConstraint(SpringLayout.NORTH, recipeAdderButtonNew, 55, SpringLayout.NORTH, recipeAdder);

		recipeAdder.add(recipeAdderButtonEdit);
		recipeAdderLayout.putConstraint(SpringLayout.WEST, recipeAdderButtonEdit, 30, SpringLayout.EAST, recipeAdderScrollPane);
		recipeAdderLayout.putConstraint(SpringLayout.NORTH, recipeAdderButtonEdit, 60, SpringLayout.NORTH, recipeAdderButtonNew);

		recipeAdder.add(recipeAdderButtonDelete);
		recipeAdderLayout.putConstraint(SpringLayout.WEST, recipeAdderButtonDelete, 30, SpringLayout.EAST, recipeAdderScrollPane);
		recipeAdderLayout.putConstraint(SpringLayout.NORTH, recipeAdderButtonDelete, 60, SpringLayout.NORTH, recipeAdderButtonEdit);

		recipeAdder.add(recipeAdderButtonInputFile);
		recipeAdderLayout.putConstraint(SpringLayout.WEST, recipeAdderButtonInputFile, 10, SpringLayout.WEST, recipeAdder);
		recipeAdderLayout.putConstraint(SpringLayout.SOUTH, recipeAdderButtonInputFile, -10, SpringLayout.SOUTH, recipeAdder);

		recipeAdder.add(recipeAdderButtonClose);
		recipeAdderLayout.putConstraint(SpringLayout.EAST, recipeAdderButtonClose, -10, SpringLayout.EAST, recipeAdder);
		recipeAdderLayout.putConstraint(SpringLayout.SOUTH, recipeAdderButtonClose, -10, SpringLayout.SOUTH, recipeAdder);
	}
	public void createRecipeAdderFrame()
	{
		mainframe.setSize(500, 510);
	}
	public void createLoginButton()
	{
		lineModeLogin = new JButton("Sign into Google");
		lineModeLogin.addActionListener(this);
		lineModeSkip = new JButton("Skip");
		lineModeSkip.addActionListener(this);
	}
	public void createLoginLabel()
	{
		loginInstructions = new JLabel("Please login to your Google Account.");
		usernameLabel = new JLabel("Username: ");
		passwordLabel = new JLabel("Password: ");
	}
	public void createLoginTextField()
	{
		userName = new JTextField();
		password = new JPasswordField();
	}
	public void createLoginPanelComponent()
	{
		labelPanel = new JPanel();
		textFieldPanel = new JPanel();
		buttonPanel = new JPanel();

		labelPanel.setLayout(new BorderLayout(0, 10));
		labelPanel.add(usernameLabel, BorderLayout.NORTH);
		labelPanel.add(passwordLabel, BorderLayout.SOUTH);

		textFieldPanel.setLayout(new BorderLayout());
		textFieldPanel.add(userName, BorderLayout.NORTH);
		textFieldPanel.add(password, BorderLayout.SOUTH);

		buttonPanel.setLayout(new BorderLayout());


		buttonPanel.add(lineModeSkip, BorderLayout.WEST);
		buttonPanel.add(lineModeLogin, BorderLayout.EAST);
	}
	public void createLoginPanel()
	{
		login = new JPanel();
		login.setLayout(new BorderLayout(0 , 20));
		login.add(loginInstructions, BorderLayout.NORTH);
		login.add(labelPanel, BorderLayout.LINE_START);
		login.add(textFieldPanel, BorderLayout.CENTER);
		login.add(buttonPanel, BorderLayout.PAGE_END);
		login.setBorder((BorderFactory.createEmptyBorder(30, 30, 30, 30)));
	}
	public void createLoginFrame()
	{
		mainframe.add(login);
		mainframe.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		mainframe.setResizable(true);
		mainframe.setSize(410, 232);
		mainframe.setVisible(true);
		mainframe.repaint();
		login.repaint();
		mainframe.getContentPane().repaint();
		login.revalidate();
	}

	public void createPrefPanels()
	{
		userPrefNext.addActionListener(this);
		eatDays.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
		eatDays.add(prefEatSundayButton);
		eatDays.add(prefEatMondayButton);
		eatDays.add(prefEatTuesdayButton);
		eatDays.add(prefEatWednesdayButton);
		eatDays.add(prefEatThursdayButton);
		eatDays.add(prefEatFridayButton);
		eatDays.add(prefEatSaturdayButton);

		grocDays.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
		grocDays.add(prefBuySundayButton);
		grocDays.add(prefBuyMondayButton);
		grocDays.add(prefBuyTuesdayButton);
		grocDays.add(prefBuyWednesdayButton);
		grocDays.add(prefBuyThursdayButton);
		grocDays.add(prefBuyFridayButton);
		grocDays.add(prefBuySaturdayButton);

		eatDaysInstructions.setLayout(new BorderLayout(0,8));
		eatDaysInstructions.add(prefEatDaysLabel, BorderLayout.NORTH);
		eatDaysInstructions.add(eatDays, BorderLayout.SOUTH);

		grocDaysInstructions.setLayout(new BorderLayout(0,8));
		grocDaysInstructions.add(prefGrocDaysLabel, BorderLayout.NORTH);
		grocDaysInstructions.add(grocDays, BorderLayout.SOUTH);

		grocFreqInstructions.setLayout(new BorderLayout(0,8));
		grocFreqInstructions.add(prefGrocWeeksLabel, BorderLayout.NORTH);
		grocFreqInstructions.add(prefGrocFrequency, BorderLayout.SOUTH);


		SpringLayout prefLayout = new SpringLayout();
		userPref = new JPanel();
		userPref.setLayout(prefLayout);
		userPref.add(eatDaysInstructions);
		prefLayout.putConstraint(SpringLayout.WEST, eatDaysInstructions, 10,  SpringLayout.WEST, userPref);
		prefLayout.putConstraint(SpringLayout.NORTH, eatDaysInstructions, 10, SpringLayout.NORTH, userPref);

		userPref.add(grocDaysInstructions);
		prefLayout.putConstraint(SpringLayout.WEST, grocDaysInstructions, 10, SpringLayout.WEST, userPref);
		prefLayout.putConstraint(SpringLayout.NORTH, grocDaysInstructions, 90, SpringLayout.NORTH, eatDaysInstructions);

		userPref.add(grocFreqInstructions);
		prefLayout.putConstraint(SpringLayout.WEST, grocFreqInstructions, 10, SpringLayout.WEST, userPref);
		prefLayout.putConstraint(SpringLayout.NORTH, grocFreqInstructions, 90, SpringLayout.NORTH, grocDaysInstructions);


		userPref.add(userPrefNext);
		prefLayout.putConstraint(SpringLayout.WEST, userPrefNext, 280, SpringLayout.WEST, userPref);
		prefLayout.putConstraint(SpringLayout.SOUTH, userPrefNext, -10, SpringLayout.SOUTH, userPref);


	}

	public String getDimensions()
	{
		return("" + mainframe.getWidth() + " " + mainframe.getHeight());
	}

	public void changeToLogin()
	{
		IngredientGUI gui = new IngredientGUI();
		gui.removeAll();
		gui.createLoginButton();
		gui.createLoginLabel();
		gui.createLoginTextField();
		gui.createLoginPanelComponent();
		gui.createLoginPanel();
		gui.createLoginFrame();
		//mainframe.setVisible(true);
	}
	public void changeToPref()
	{
		mainframe.getContentPane().removeAll();
		mainframe.setSize(new Dimension(640, 350));
		createPrefPanels();
		mainframe.add(userPref);
		mainframe.setVisible(true);
		//mainframe.revalidate();
		mainframe.repaint();
		userPref.repaint();
		mainframe.getContentPane().repaint();
		userPref.revalidate();
	}
	public void changeToRecipe()
	{
		String [] temp = null;
		try
		{
			temp = DataBase.getMealName();
		}
		catch(Exception e){}
		recipeModel.clear();
		for(int i = 0; i < temp.length; i++)
		{
			recipeModel.addElement(temp[i]);
		}
		mainframe.getContentPane().removeAll();
		createRecipeButton();
		createRecipeAdderPanel();
		createRecipeAdderFrame();
		mainframe.add(recipeAdder);
		mainframe.setVisible(true);
		recipeAdder.repaint();
		mainframe.repaint();
		mainframe.getContentPane().repaint();
		recipeAdder.revalidate();
	}

	public void changeToIngredient()
	{
		mainframe.getContentPane().removeAll();
		createIngredientTextField();
		createIngredientButton();
		createIngredientLabel();
		createIngredient();
		mainframe.add(ingredientAdder);
		//mainframe.revalidate();
		mainframe.setVisible(true);
		mainframe.repaint();
		ingredientAdder.repaint();
		ingredientAdder.revalidate();
		mainframe.getContentPane().repaint();
		//FOCUS HERE
		recipeName.requestFocus();
	}
	public void actionPerformed(ActionEvent e)
	{
		Object clicked = e.getSource();

		if(clicked == lineModeLogin)
		{

			if(!(CalendarInterface.initService(userName.getText(), new String(password.getPassword()))))
			{
				JOptionPane.showMessageDialog(null, "Wrong Username/Password", "Wrong Username/Password", JOptionPane.ERROR_MESSAGE);
				password.setText("");
				return;
			}

			for (int i = 0; i < password.getPassword().length; i++)
			{
				password.getPassword()[i] = 0;
			}
			changeToRecipe();
			SysTray.login();
		}
		else if(clicked == userPrefNext)
		{
			//prefEatSaturdayButton

			//changeToRecipe();

			util.UserData.setDaysToGenerate(new boolean[] {
				prefEatSundayButton.isSelected(),
				prefEatMondayButton.isSelected(),
				prefEatTuesdayButton.isSelected(),
				prefEatWednesdayButton.isSelected(),
				prefEatThursdayButton.isSelected(),
				prefEatFridayButton.isSelected(),
				prefEatSaturdayButton.isSelected()
			});
			mainframe.setVisible(false);
		}
		else if(clicked == recipeAdderButtonClose)
		{
			System.exit(0);
		}
		else if(clicked == ingredientAdderClose)
		{
			changeToRecipe();
		}
		else if(clicked == recipeAdderButtonEdit)
		{
			changeToIngredient();

			ingredientModel.clear();
			instructionModel.clear();
			tempIngredientList.clear();
			tempInstructionList.clear();
			String tempRecipeName = (String) recipeAdderList.getSelectedValue();
			System.out.println(tempRecipeName);
			Meal tempMeal = null;
			try
			{
				tempMeal = DataBase.getMeal(tempRecipeName);
			}
			catch(Exception f){}

			recipeName.setText(tempRecipeName);
			rating.setSelectedIndex((int) tempMeal.getRating());

			Ingredient[] temp = tempMeal.getIngredients();
			for(int i = 0; i < temp.length; i++)
			{
				ingredientModel.addElement(temp[i].getQuantity() + " " + temp[i].getUnits() + " of " + temp[i].getName());
				tempIngredientList.add(temp[i]);
			}
			for(Direction dir : tempMeal.getDirections()) {
				instructionModel.addElement(dir.getStep());
				tempInstructionList.add(dir);
			}


		}
		else if(clicked == recipeAdderButtonNew)
		{
			changeToIngredient();
		}
		else if(clicked == ingredientAdderSave)
		{
			System.out.println(tempIngredientList.get(0));
			Meal noMagic = new Meal(recipeName.getText(), tempIngredientList.toArray(new Ingredient[] {null}), tempInstructionList.toArray(new Direction[] {null}), rating.getSelectedIndex());
			try
			{
				DataBase.saveMeal(noMagic);
			}
			catch(Exception f)
			{
				f.printStackTrace();
			}
			changeToRecipe();
			ingredientModel.clear();
			instructionModel.clear();
			tempIngredientList.clear();
			tempInstructionList.clear();

		}
		else if((clicked == ingredientAdd) || (clicked == ingredientName))
		{
			if(!isEdit)
			{
				ingredientModel.addElement(ingredientQuantity.getText() + " " + units.getSelectedItem() + " of " + ingredientName.getText());
				tempIngredientList.add(new Ingredient(ingredientName.getText(), Integer.parseInt(ingredientQuantity.getText()), ""+units.getSelectedItem()));
				ingredientQuantity.setText("");
				ingredientName.setText("");
				units.addItem(units.getSelectedItem());
				units.setSelectedIndex(0);
			}
			else
			{
				int index = ingredientList.getSelectedIndex();
				ingredientModel.set(index, ingredientQuantity.getText() + " " + units.getSelectedItem() + " of " + ingredientName.getText());
				tempIngredientList.set(index, new Ingredient(ingredientName.getText(), Integer.parseInt(ingredientQuantity.getText()), ""+units.getSelectedItem()));
				ingredientQuantity.setText("");
				ingredientName.setText("");
				units.addItem(units.getSelectedItem());
				units.setSelectedIndex(0);
				isEdit = false;
			}
			ingredientQuantity.requestFocus();
		}
		else if (clicked == ingredientEdit)
		{
			ingredientQuantity.setText("");
			ingredientName.setText("");
			units.setSelectedIndex(0);
			Ingredient temp = tempIngredientList.get(ingredientList.getSelectedIndex());
			ingredientQuantity.setText(""+temp.getQuantity());
			units.setSelectedItem(""+temp.getUnits());
			ingredientName.setText(""+temp.getName());
			isEdit = true;
		}
		else if((clicked == ingredientDelete) && (ingredientList.getFirstVisibleIndex() != -1))
		{
			int index = ingredientList.getSelectedIndex();
			ingredientQuantity.setText("");
			ingredientName.setText("");
			units.setSelectedIndex(0);
			ingredientModel.remove(index);
			tempIngredientList.remove(index);
			if((index < ingredientList.getModel().getSize()))
			{
				ingredientList.setSelectedIndex(index);
			}
		}
		else if(clicked == lineModeSkip)
		{
			changeToPref();
		}
		else if((clicked == instructionAdd) || (clicked == instructionStep))
		{

			if(!isInstructionEdit)
			{
				instructionModel.addElement(instructionStep.getText());
				tempInstructionList.add(new Direction(instructionStep.getText()));
				instructionStep.setText("");
			}
			else
			{
				int index = instructionList.getSelectedIndex();
				instructionModel.set(index, instructionStep.getText());
				tempInstructionList.set(index, new Direction(instructionStep.getText()));
				instructionStep.setText("");
				isInstructionEdit = false;
			}
			instructionStep.requestFocus();
		}
		else if (clicked == instructionEdit)
		{
			instructionStep.setText("");
			Direction temp = tempInstructionList.get(instructionList.getSelectedIndex());
			instructionStep.setText(temp.getStep());
			isInstructionEdit = true;
		}
		else if((clicked == instructionDelete) && (instructionList.getFirstVisibleIndex() != -1))
		{
			int index = instructionList.getSelectedIndex();
			instructionModel.remove(index);
			tempInstructionList.remove(index);
			if(index < instructionList.getModel().getSize())
			{
				instructionList.setSelectedIndex(index);
			}
		}
	}
}
